home *** CD-ROM | disk | FTP | other *** search
/ TOS Silver 2000 / TOS Silver 2000.iso / programm / GNUC / UTIL-41S.LZH / util-41 / toglclr.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-10-05  |  6.6 KB  |  288 lines

  1. /*                             -*- Mode: Elec-C -*- 
  2.  * toglclr.c -- utility to toggle the program load flags in the .prg header.
  3.  *
  4.  *              usage: toglclr [options] file file ...
  5.  * 
  6.  * Author          : J.R. Bammi, modified by F. Ridderbusch
  7.  * Created On      : Some time ago.
  8.  * Last Modified By: Frank Ridderbusch
  9.  * Last Modified On: Wed Apr 14 21:26:12 1993
  10.  * Update Count    : 18
  11.  * Status          : Unknown, Use with caution!
  12.  */
  13.  
  14. /* HISTORY 
  15.  * 14-Apr-1993        Frank Ridderbusch    
  16.  *    Added code to handle the memory protection bits under MultiTOS.
  17.  *    Changed output format slightly.
  18.  * 31-Oct-1992        Thomas Schulze
  19.  *    Added code for MiNT 0.96 (and up) shared text feature. -fshared
  20.  *    will switch this flag.
  21.  * 25-Sep-1992          ++jrb
  22.  *    Added support for cross-environment with support for 
  23.  *    WORD_ALIGNED hosts.
  24.  * 13-Sep-1992        Frank Ridderbusch    
  25.  *    This program was originally written by J.R. Bammi
  26.  *    (bammi@cadence.com) to toggle the clear TPA above BSS flag
  27.  *    introduced with TOS 1.4. I extended it to also handle the additional
  28.  *    program flags introduced with the TOS versions for the STE and TT.
  29.  */
  30.  
  31. #include <stdio.h>
  32. #include <stdlib.h>
  33. #include <unistd.h>
  34. #include <string.h>
  35.  
  36. #ifdef CROSSATARI
  37. #include "cross-inc/st-out.h"
  38. #else
  39. #include <st-out.h>
  40. #endif
  41.  
  42. #ifndef FILENAME_MAX
  43. #define FILENAME_MAX 1024
  44. #endif
  45.  
  46. /*
  47.  * ldflgs - stolen from MiNT mem.h
  48.  */
  49.  
  50. #define    F_PROTMODE    0xf0        /* protection mode bits */
  51. #define F_PROT_P    0x00        /* no read or write */
  52. #define F_PROT_G    0x10        /* any access OK */
  53. #define F_PROT_S    0x20        /* any super access OK */
  54. #define F_PROT_PR    0x30        /* any read OK, no write */
  55.  
  56. int flags_to_toggle = 0;
  57. int set_mp_flags = 0;
  58.  
  59. extern void dump_version(char *prog);
  60. char *progname = "toglclr";
  61.  
  62. #ifdef WORD_ALIGNED
  63.  
  64. #define ckread(addr, bytes, fd) if(read(fd, addr, bytes) != bytes) return 1;
  65. #define ckwrite(addr, bytes, fd) if(write(fd, addr, bytes) != bytes) return 1;
  66.  
  67. int readhead(h, fd)
  68. struct aexec *h;
  69. int fd;
  70. {
  71.     short i;
  72.     long j;
  73.     int k;
  74.     
  75.     ckread(&i, 2, fd);
  76.     h->a_magic = i;
  77.     ckread(&j, 4, fd);
  78.     h->a_text = j;
  79.     ckread(&j, 4, fd);
  80.     h->a_data = j;
  81.     ckread(&j, 4, fd);
  82.     h->a_bss = j;
  83.     ckread(&j, 4, fd);
  84.     h->a_syms = j;
  85.     ckread(&j, 4, fd);
  86.     h->a_AZero1 = j;
  87.     ckread(&j, 4, fd);
  88.     h->a_ldflgs = j;
  89.     ckread(&i, 2, fd);
  90.     h->a_isreloc = i;
  91.  
  92.     return 0;
  93. }
  94.  
  95. int writehead(h, fd)
  96. struct aexec *h;
  97. int fd;
  98. {
  99.     short i;
  100.     long j;
  101.     int k;
  102.     
  103.     i = h->a_magic;
  104.     ckwrite(&i, 2, fd);
  105.     j = h->a_text;
  106.     ckwrite(&j, 4, fd);
  107.     j = h->a_data;
  108.     ckwrite(&j, 4, fd);
  109.     j = h->a_bss;
  110.     ckwrite(&j, 4, fd);
  111.     j = h->a_syms;
  112.     ckwrite(&j, 4, fd);
  113.     j = h->a_AZero1;
  114.     ckwrite(&j, 4, fd);
  115.     j = h->a_ldflgs;
  116.     ckwrite(&j, 4, fd);
  117.     i = h->a_isreloc;
  118.     ckwrite(&i, 2, fd);
  119.  
  120.     return 0;
  121. }
  122.  
  123. #else
  124.  
  125. #define readhead(addr, fd) \
  126.  (read(fd, addr, sizeof(struct aexec)) != sizeof(struct aexec))
  127.  
  128. #define writehead(addr, fd) \
  129.  (write(fd, addr, sizeof(struct aexec)) != sizeof(struct aexec))
  130.  
  131. #endif /* WORD_ALIGNED */
  132.  
  133. int toggle (fd, fn)
  134. int fd;
  135. char *fn;
  136. {
  137.     struct aexec head;
  138.     unsigned long t;
  139.     char *ptr;
  140.     
  141.     if(readhead(&head, fd))
  142.     {
  143.     perror(fn);
  144.     return 4;
  145.     }
  146.     if(head.a_magic != CMAGIC)
  147.     {
  148.     fprintf(stderr,"%s: Invalid magic number %x\n", fn, head.a_magic);
  149.     return 8;
  150.     }
  151.  
  152.     t = head.a_AZero2;
  153.     if (flags_to_toggle || set_mp_flags)
  154.     {
  155.         head.a_AZero2 &= ~F_PROTMODE;
  156.         head.a_AZero2 ^= flags_to_toggle;
  157.         lseek(fd, 0L, SEEK_SET);
  158.     if(writehead(&head, fd))
  159.         {
  160.         perror(fn);
  161.         return 16;
  162.         }
  163.     }
  164.  
  165.     printf("%s:\n\t           `fast load' bit was %d, is now %d.\n", fn, 
  166.        (int)((t & F_FASTLOAD) == F_FASTLOAD),
  167.        (int)((head.a_AZero2 & F_FASTLOAD) == F_FASTLOAD));
  168.  
  169.     printf("\t     `run in fast ram' bit was %d, is now %d.\n",
  170.        (int)((t & F_ALTLOAD) == F_ALTLOAD),
  171.        (int)((head.a_AZero2 & F_ALTLOAD) == F_ALTLOAD));
  172.  
  173.     printf("\t`malloc from fast ram' bit was %d, is now %d.\n",
  174.        (int)((t & F_ALTALLOC) == F_ALTALLOC),
  175.        (int)((head.a_AZero2 & F_ALTALLOC) == F_ALTALLOC));
  176.  
  177.     printf("\t         `shared text' bit was %d, is now %d.\n",
  178.         (int)((t & F_SHTEXT) == F_SHTEXT),
  179.         (int)((head.a_AZero2 & F_SHTEXT) == F_SHTEXT));
  180.  
  181.     switch (head.a_AZero2 & F_PROTMODE)
  182.     {
  183.         case F_PROT_P:
  184.             ptr = "private";
  185.         break;
  186.         case F_PROT_G:
  187.             ptr = "global";
  188.         break;
  189.         case F_PROT_S:
  190.             ptr = "super";
  191.         break;
  192.         case F_PROT_PR:
  193.             ptr = "private/readable";
  194.         break;
  195.     }
  196.     printf("\truns now with `%s' memory protection.\n", ptr);
  197.     return 0;
  198. }
  199.  
  200. int main(argc, argv)
  201. int argc;
  202. char **argv;
  203. {
  204.     int fd;
  205.     int tmp = 0;
  206.     int status = 0;
  207.     char fn[FILENAME_MAX];
  208.     
  209.      if (argv[0][0] != '\0')
  210.        progname = argv[0];
  211.     
  212.      if (argc == 2 && strcmp(argv[1], "-v") == 0)
  213.      {
  214.        dump_version(progname);
  215.        exit(0);
  216.      }
  217.  
  218.     if(argc < 2)
  219.     {
  220.     fprintf(stderr, "usage: toglclr [options] file file .....\n\n");
  221.     fprintf(stderr, "options: -fload    = toggle `fast load' bit\n");
  222.     fprintf(stderr, "         -frun     = toggle `load program into fast ram' bit\n");
  223.     fprintf(stderr, "         -fram     = toggle `malloc from fast ram' bit\n");
  224.     fprintf(stderr, "         -fshare   = toggle `shared text' bit\n\n");
  225.     fprintf(stderr, "options for memory protection (only one of these allowed and\n");
  226.         fprintf(stderr, "        only sensible under MultiTOS):\n");
  227.     fprintf(stderr, "         -private  = \n");
  228.     fprintf(stderr, "         -global   = \n");
  229.     fprintf(stderr, "         -super    = \n");
  230.     fprintf(stderr, "         -readable = \n\n");
  231.     fprintf(stderr, "without options the current state is reported.\n");
  232.     exit(1);
  233.     }
  234.  
  235.     while (**++argv == '-')
  236.     {
  237.         if (!strcmp(*argv, "-fload"))
  238.         flags_to_toggle |= F_FASTLOAD;
  239.         if (!strcmp(*argv, "-frun"))
  240.         flags_to_toggle |= F_ALTLOAD;
  241.         if (!strcmp(*argv, "-fram"))
  242.         flags_to_toggle |= F_ALTALLOC;
  243.     if (!strcmp(*argv, "-fshare"))
  244.         flags_to_toggle |= F_SHTEXT;
  245.     if (!strcmp(*argv, "-private"))
  246.     {
  247.         tmp = F_PROT_P;
  248.         set_mp_flags = 1;
  249.     }
  250.     if (!strcmp(*argv, "-global"))
  251.     {
  252.         tmp = F_PROT_G;
  253.         set_mp_flags = 1;
  254.     }
  255.     if (!strcmp(*argv, "-super"))
  256.     {
  257.         tmp = F_PROT_S;
  258.         set_mp_flags = 1;
  259.     }
  260.     if (!strcmp(*argv, "-readable"))
  261.     {
  262.         tmp = F_PROT_PR;
  263.         set_mp_flags = 1;
  264.     }
  265.     --argc;
  266.     }
  267.     
  268.     flags_to_toggle |= tmp;
  269.  
  270.     while(--argc > 0)
  271.     {
  272.     (void) strcpy(fn, *argv++);
  273.         if((fd = open(fn, 2)) < 0)
  274.         {
  275.         perror(fn);
  276.         continue;
  277.         }
  278.     status |= toggle(fd, fn);
  279.     if(close(fd))
  280.     {
  281.         perror(fn);
  282.         exit(2);
  283.     }
  284.     }
  285.     
  286.     return status;
  287. }
  288.